//
//  ProfileUserCell.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/24.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

private extension String {
    var infoString: NSAttributedString {
        guard contains("\n") else {
            assert(false, "string 必须包含换行符 \n")
            return NSAttributedString()
        }
        guard count != 2 else {
            assert(false, "string 必须包含换行符 \n")
            return NSAttributedString()
        }
        let strs = components(separatedBy: "\n")
        let topFont = UIFont.systemMediumFont(withSize: 16)
        let bottomFont = UIFont.systemRegularFont(withSize: 10)
        let topStr = NSMutableAttributedString(string: strs[0], attributes: [.font: topFont, .foregroundColor: UIColor.white])
        let bottomStr = NSAttributedString(string: "\n" + strs[1], attributes: [.font: bottomFont])
        topStr.append(bottomStr)

        let style = NSMutableParagraphStyle()
        style.lineSpacing = 5
        topStr.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: topStr.length))

        return topStr.copy() as! NSAttributedString
    }
}

class ProfileUserCell: UITableViewCell {

    enum Mode{
        enum InfoMode {
            case part
            case all
        }
        case info(InfoMode)
        case tag
    }

    var addTagAction: VoidBlock?

    private var lastMode: Mode?

    @IBOutlet weak var tagsView: ProfileUserTagsView!
    @IBOutlet weak var modeButton: UIButton!
    @IBOutlet weak var bgImageView: UIImageView!
    @IBOutlet weak var switchButton: UIButton!
    @IBOutlet weak var avatarView: ProfileUserAvatarView!
    @IBOutlet weak var accountView: ProfileUserAccountView!
    @IBOutlet weak var infosView: UIView!
    @IBOutlet weak var infosAllView: ProfileUserInfosAllView!
    @IBOutlet weak var infosPartView: ProfileUserInfosPartView!

    var mode: Mode = .info(.all) {
        didSet {
            UIView.animate(withDuration: 0.25, animations: {
                switch self.mode {
                case .info(let info):
                    switch info {
                    case .all:
                        self.switchButton.setTitle("显示\n全部", for: .normal)
                        self.switchButton.setBackgroundImage(#imageLiteral(resourceName: "profile_user_btn_bg"), for: .normal)
                        self.modeButton.setTitle("切换到标签", for: .normal)
                        self.tagsView.alpha = 0
                        self.infosView.alpha = 1
                        self.infosAllView.alpha = 1
                        self.infosPartView.alpha = 0
                    case .part:
                        self.switchButton.setBackgroundImage(#imageLiteral(resourceName: "profile_user_btn_bg"), for: .normal)
                        self.switchButton.setTitle("隐藏\n部分", for: .normal)
                        self.modeButton.setTitle("切换到标签", for: .normal)
                        self.tagsView.alpha = 0
                        self.infosView.alpha = 1
                        self.infosAllView.alpha = 0
                        self.infosPartView.alpha = 1
                    }
                case .tag:
                     self.switchButton.setTitle("", for: .normal)
                    self.switchButton.setBackgroundImage(#imageLiteral(resourceName: "profile_user_btn_add"), for: .normal)
                    self.modeButton.setTitle("切换到信息", for: .normal)
                    self.infosView.alpha = 0
                    self.tagsView.alpha = 1
                }
            })
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        infosPartView.alpha = 0
        tagsView.alpha = 0

        accountView.balance = "898.90"
        accountView.isSecrect = false

        switchButton.titleLabel?.numberOfLines = 2
        bgImageView.contentMode = .scaleAspectFill
        bgImageView.clipsToBounds = true
        bgImageView.image = #imageLiteral(resourceName: "头像").byBlurSoft()

        mode = .info(.all)
    }

    //切换全部/部分信息
    @IBAction func switchButtonClicked(_ sender: Any) {
        switch mode {
        case .info(let info):
            switch info {
            case .all: mode = .info(.part)
            case .part: mode = .info(.all)
            }
        case .tag:
            //添加标签
            addTagAction?()
        }
    }

    //切换到标签或者信息
    @IBAction func modeButtonClicked(_ sender: Any) {
        switch mode {
        case .info: mode = .tag
        case .tag: mode = lastMode ?? .info(.all)
        }
        lastMode = mode
    }
}

class ProfileUserAvatarView: UIView {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var rankLabel: UILabel!
    @IBOutlet weak var departmentLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        let image = #imageLiteral(resourceName: "头像")
        let avatarImage = image.byRoundCornerRadius(120, borderWidth: 3, borderColor: UIColor.lightText)
        imageView.image = avatarImage

    }
}

class ProfileUserInfosAllView: UIView {
    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var contractNumberLabel: UILabel!
    @IBOutlet weak var prefixLabel: UILabel!
    @IBOutlet weak var amountLabel: UILabel!
    @IBOutlet weak var numberLabel: UILabel!
    @IBOutlet weak var priceTypeLabel: UILabel!
    @IBOutlet weak var shopNumberLabel: UILabel!
    @IBOutlet weak var affiliationLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()

        typeLabel.attributedText = "000B\n默认衬类型".infoString
        contractNumberLabel.attributedText = "111\n合同号".infoString
        prefixLabel.attributedText = "TEST\n订单号前缀".infoString
        amountLabel.attributedText = "¥ 15000\n停用金额".infoString
        numberLabel.attributedText = "3213213123\n量体编号".infoString
        priceTypeLabel.attributedText = "面料加工费定价规范\n定价规范".infoString
        shopNumberLabel.attributedText = "k231232\n门店".infoString
        affiliationLabel.attributedText = "信息部测试账号\n客户公司".infoString

    }
}

class ProfileUserInfosPartView: UIView {
    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var contractNumberLabel: UILabel!
    @IBOutlet weak var prefixLabel: UILabel!
    @IBOutlet weak var amountLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        typeLabel.attributedText = "000B\n默认衬类型".infoString
        contractNumberLabel.attributedText = "111\n合同号".infoString
        prefixLabel.attributedText = "TEST\n订单号前缀".infoString
        amountLabel.attributedText = "¥ 15000\n停用金额".infoString

    }
}

class ProfileUserAccountView: UIView {

    @IBOutlet weak var numberButton: UIButton!
    @IBOutlet weak var balanceButton: UIButton!
    @IBOutlet weak var prepayButton: UIButton!
    @IBOutlet weak var cashButton: UIButton!

    var balance: String?

    var isSecrect = false {
        didSet {
            balanceButton.isSelected = isSecrect
            if isSecrect {
                numberButton.titleLabel?.text = "¥*****"
                numberButton.setTitle("¥*****", for: .normal)
            } else {
                numberButton.titleLabel?.text = "¥\(balance ?? "未知")" //防止闪烁
                numberButton.setTitle("¥\(balance ?? "未知")", for: .normal)
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        prepayButton.setBackgroundImage(UIImage.redRoundedImage, for: .normal)
        prepayButton.setTitle("充值", for: .normal)
    }

    @IBAction func balanceButtonClicked(_ sender: Any) {
        isSecrect = !isSecrect
    }
}
